
import { resources, Vec3, Node, Prefab, instantiate, Label, tween, pseudoRandomRangeInt, randomRangeInt, pseudoRandomRange, error, Vec2, JsonAsset, UITransform, math } from "cc"
import { Enemy } from "./Enemy";
import { Battle } from "./Battle";
import { Enum_resourcesPrefab } from "./Enum";
import { NodePoolManager } from "./NodePoolManager";
import { Globals } from "./Globals";

export class Util {
    // 创建伤害数字的函数
    /**
     * 创建伤害数字的函数
     * @param text 文本
     * @param color 文本颜色
     * @param worldPosition 其世界坐标
     * @param parentNode 其父节点
     */
    static damageTextFun(text: string, color: string, worldPosition: Vec3, parentNode: Node) {
        // 实例化damgeText节点，并设置文字，父节点，世界坐标
        const damageTextNode = Globals.getNode("DamageText", parentNode);
        damageTextNode.getComponent(Label).string = text;
        damageTextNode.setWorldPosition(worldPosition);
        // 设置颜色
        damageTextNode.getComponent(Label).color = new math.Color(color);
        // 缓动，放大然后缩小，再销毁
        damageTextNode.scale = new Vec3(1, 1, damageTextNode.scale.z);
        tween(damageTextNode)
            .by(.2, { scale: new Vec3(1.2, 1.2, damageTextNode.scale.z) })
            .to(.3, { scale: new Vec3(0.1, 0.1, damageTextNode.scale.z) })
            .call(() => {
                damageTextNode.destroy();
            })
            .start()

    }

    // // 加载prefab(异步加载prefab)
    // /**
    //  * 异步加载resources中的prefab
    //  * @param prefabUrl 动态加载预制体的路径
    //  * @returns 返回Promise结果，成功就携带此prefab
    //  */
    // static loadPrefabs(prefabUrl: string): Promise<any> {
    //     return new Promise((resolve, reject) => {
    //         resources.load(prefabUrl, (err: Error, EnemyPrefab: Prefab) => {
    //             if (err) {
    //                 error(err.message);
    //                 reject && reject(err);
    //                 return;
    //             }
    //             resolve && resolve(EnemyPrefab);
    //         })
    //     })
    // }



    // 通过预制体创建敌人1
    /**
     * 通过预制体创建敌人1
     * @param EnemyPrefab 敌人预制体
     * @param parentNode 挂载的父节点
     * @param worldPosition 其世界坐标
     * @return EnemyNode 返回的敌人节点
     */
    static enemy01Fun(EnemyPrefab: Prefab, parentNode: Node, worldPosition?: Vec3) {
        //实例化
        const EnemyNode = instantiate(EnemyPrefab);
        EnemyNode.setParent(parentNode);
        worldPosition && EnemyNode.setPosition(worldPosition);
        return EnemyNode;
    }


    // 节点移动函数
    /**
     * 
     * @param node 要移动的节点
     * @param radian 弧度
     * @param speed 速度
     */
    static nodeMove(node: Node, radian: number, speed: number) {
        // 移动后的值
        let x = node.position.x + Math.cos(radian) * speed;
        let y = node.position.y + Math.sin(radian) * speed;

        if (node.name === "Player") {
            const nodeSize = node.getComponent(UITransform).contentSize;
            const mapSize = Battle.MapNode.getComponent(UITransform).contentSize;
            // 边界值
            const xBorder = mapSize.width / 2 - nodeSize.width / 2;
            const yBorder = mapSize.height / 2 - nodeSize.height / 2;
            if (x < -xBorder || x > xBorder) {
                x = node.position.x;
            }
            if (y < -yBorder || y > yBorder) {
                y = node.position.y;
            }
        }
        node.setPosition(x, y);
    }


}